
/** OPTIONS, LIBREFS **/
OPTIONS MPRINT NOFMTERR PS=60 LS=80 sasautos =macro;
FILENAME macro "/disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/macro";
libname rg '/disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/measures/tables/regression/rru';
libname res '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results';
libname wk '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results';
libname wk9 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2009';
libname wk8 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2008';
libname wk7 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2007';
libname wk6 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2006';
libname wk5 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2005';
libname wk4 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2004';
libname wk3 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2003';
libname ma '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/MA_files';
libname c '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/cardiac/2007';
libname d '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/2007';


***PPO PLAN CHARACTERISTICS;

data ppo;
 set rg.ppo_char;
 if newsince2006 = 1 then ppo_new = 1; else ppo_new = 0;
 if taxstatus = "PRO" then ppo_prof = 1; else ppo_prof = 0;
 if plansize = "Small<25k" then ppo_small = 1; else ppo_small = 0;
 if newsince2006 = . then delete;
 ppo_size = tot_n;
 keep ppo_size org_id ppo_small ppo_prof ppo_new;
proc sort;
 by org_id;


***PLAN LEVEL CARDIAC & DIABETES RESULTS;
data res;
 length var $ 9;
 set c.cardiac_results_new (in=c)
 d.diab_results_new (in=d);
if var = "ed_vi" then var = "ed_vis";
if var = "em_co" then var = "em_cost";
if var = "ip_co" then var = "ip_cost";
if var = "surg_" then var = "surg_cost";
if var = "ip_vi" then var = "ip_vis";

if d then meas = compress("diab"||"_"||var);
else if c then meas = compress("card"||"_"||var);
if hmo_plan_mean in (.,0) or ffs_plan_mean in (.,0) then delete;

drop var;
proc sort;
 by org_id;


data deb;
 set res.plans_debut (keep = org_id debut);
proc sort;
 by org_id;

data pln;
 set res.hmoplans;
proc sort;
 by org_id;

data size;
 set res.plan_size;
proc sort;
 by org_id;


data r;
 set ma.planname (keep = org_id org_name);
proc sort;
 by org_id;


data mg;
 merge res (in=r) ppo (in=pp) deb pln size r;
 by org_id;
 if r=1;
 if pp then ppo = 1; else ppo = 0;

if debut in (2006, 2007, 2008, 2009) then new = 1; else new = 0;
if ppo = 1 and ppo_new = 1 then new = 1;
 else if ppo = 1 and ppo_new = 0 then new = 0;


%macro y(yr);
%macro m(meas);
 
data mg;
 set mg;

 affiliation = affiliation07;
 taxstatus=taxstatus07;
 size = pats07;
if ppo = 0 and (affiliation= "" or taxstatus="") then delete;
if ppo = 1 then size = ppo_size;


***SPLIT FILES INTO HMO & FFS;

data ffs (keep = small national forprofit ppo ffs_plan_mean ffs_plan_se org_id size new rename = (ffs_plan_mean=rate ffs_plan_se=se))
 hmo (keep = small national forprofit ppo  hmo_plan_mean hmo_plan_se org_id size new rename = (hmo_plan_mean=rate hmo_plan_se=se));
 set mg ;
 where meas = "&meas.";

if taxstatus = "NON" then nonprofit = 1; else nonprofit = 0;
if affiliation = "Local" then local = 1; else local = 0;

if taxstatus = "NON" then forprofit = 0; else forprofit = 1;
if affiliation = "Local" then national = 0; else national = 1;

if size > = 25000 then small = 0;
 else small = 1;

if ppo = 1 and ppo_small = 1 then small = 1;
 else if ppo  = 1 and ppo_small = 0 then small = 0;

if ppo = 1 and ppo_prof = 1 then forprofit = 1;
 else if ppo = 1 and ppo_prof = 0 then forprofit = 0;


 output ffs;
 output hmo;


 
***MERGE FILES FOR MODEL;

data all;
 set ffs (in=f) hmo (in=h);
 if f then type = 1;
 if h then type = 0;

    y=rate/se ;
    ma = (1-type)/se;
    ffs = type/se;

   manational=ma*national ;

   ffsnational=ffs*national ;

   maforprofit=ma*forprofit;

   ffsforprofit=ffs*forprofit;
   
   ma_small=small*ma;
   ffs_small=small*ffs;

   ma_new = new * ma;
   ffs_new = new * ffs;

   ma_ppo = ppo * ma;
   ffs_ppo = ppo * ffs;

run;





%macro tp(wt,lab);
data all;
 set all;
 one = 1;
 size2=size;
 one =size; 
 size = size;
 if type = 1 then tpp = "2.ffs";
 else tpp = "1.hmo";
run;

proc means data=all n nmiss mean p25 p50 p75;
 var rate; 
 class tpp;
 title "&meas";

proc means data=all n nmiss mean p25 p50 p75;
 var rate;
 class tpp;
 weight size2;
 title "&meas, wegihted";


run;


ods output covparms = cov
           solutionf = f;
proc mixed data=all empirical convh=0.000002;
    class org_id ;
    model y = ma ffs ma_small  ffs_small ma_new ffs_new
    maforprofit ffsforprofit ma_ppo ffs_ppo / noint solution ;
    random ma ffs / subject=org_id type = un ;
    weight &wt;
    parms (.003) (0) (.001) (1) / hold=4 ;
 title "&meas. 200&yr.";
run; quit;
ods output close;


**************;
***COVPARMS***;
**************;

data cov&meas.&lab.&yr. (keep = est_&meas.&yr. year effect);
 length year $ 4;
 set cov;
 year = "200&yr.";
 if covparm = "Residual" then delete;
 rename covparm=effect
 estimate=est_&meas.&yr.;

proc sort;
 by effect;

**************;
***SOLUTION***;
**************;

data f&meas.&lab.&yr. (keep = est_&meas.&yr. year effect);
 length year $ 4;
 set f;
 year = "200&yr.";
 rename estimate=est_&meas.&yr.;
proc sort;
 by effect;

**************;
***COMBINED***;
**************;

data un21 (keep = mg est_&meas.&yr. rename = (est_&meas.&yr. = &meas._un21)) 
 un22 (keep = mg est_&meas.&yr rename = (est_&meas.&yr. = &meas._un22)) 
 ma (keep = mg est_&meas.&yr rename = (est_&meas.&yr. = &meas._ma)) 
 ffs (keep = mg est_&meas.&yr rename = (est_&meas.&yr. = &meas._ffs));
 length effect $ 12;
 
 set cov&meas.&lab.&yr.  f&meas.&lab.&yr.;
 mg = 1;
 if effect = "UN(2,1)" then output un21;
 if effect = "UN(2,2)" then output un22;
 if effect = "ma" then output ma;
 if effect = "ffs" then output ffs;

proc sort data=un21;
 by mg;
proc sort data=un22;
 by mg;
proc sort data=ma;
 by mg;
proc sort data=ffs;
 by mg;

data mg&lab (keep = &lab.slope &lab.intercept meas year);
 length meas $ 12;
 merge un21 un22 ma ffs;
 by mg;
 meas = "&meas";
 year = 200&yr.;

&lab.slope = &meas._un21/&meas._un22;
&lab.intercept = &meas._ma - (&lab.slope*&meas._ffs);

proc append data=mg&lab base=slp&lab.;
%mend tp;

%tp(size,w)
%tp(one,m);

%mend m;

%m(card_ed_vis)
%m(card_ip_vis)
%m(card_em_cost)
%m(card_ip_cost)
%m(card_surg_cost)

%m(diab_ed_vis)
%m(diab_ip_vis)
%m(diab_em_cost)
%m(diab_ip_cost)
%m(diab_surg_cost)



%mend y;

%y(9)


%macro tt(t);

data cov&t.;
 merge cov&t.m9;
 by effect;


data f&t.;
 merge f&t.m9;
 by effect;

data all&t.;
 length effect $ 12;
 set cov&t. f&t.;
 if effect = "UN(1,1)" then sort = 1;
 if effect = "UN(2,1)" then sort = 2;
 if effect = "UN(2,2)" then sort = 3;
 if effect = "ma" then sort = 4;
 if effect = "ffs" then sort = 5;
 if effect = "manational" then sort = 6;
 if effect = "ffsnational" then sort = 7;
 if effect = "maforprofit" then sort = 8;
 if effect = "ffsforprofit" then sort = 9;
 if effect = "ma_small" then sort = 10;
 if effect = "ffs_small" then sort = 11;
 if effect = "ma_new" then sort = 12;
 if effect = "ffs_new" then sort = 13;
 if effect = "ma_ppo" then sort = 14;
 if effect = "ffs_ppo" then sort = 15;

proc sort;
 by sort;


%mend tt;


%tt(card_ed_vis)
%tt(card_ip_vis)
%tt(card_em_cost)
%tt(card_ip_cost)
%tt(card_surg_cost)

%tt(diab_ed_vis)
%tt(diab_ip_vis)
%tt(diab_em_cost)
%tt(diab_ip_cost)
%tt(diab_surg_cost)


data rg.results;
 merge  allcard_ed_vis allcard_ip_vis allcard_em_cost allcard_ip_cost allcard_surg_cost
 alldiab_ed_vis alldiab_ip_vis alldiab_em_cost alldiab_ip_cost alldiab_surg_cost;
 by sort;
run;


ods html file= "regression.xls";
proc print noobs data=rg.results;
title "mixed model results";

run; quit;
ods html close;



proc sort data=slpw;
 by meas year;
proc sort data=slpm;
 by meas year;

proc sort data=mg;
 by meas year;

data mg;
 merge mg slpm slpw;
 by meas year;


%macro yy(y);
%macro m(ms);
data mg2;
 set mg;
 if year = 200&y and meas = "&ms.";

if taxstatus0&y = "PRO" then forprofit = 1;
else forprofit = 0;

data mg2;
 set mg2; 
file "&ms.0&y..txt";
 if _n_ = 1 then do;
 put "hmo, " @;
 put "hmo_lb, " @;
 put "hmo_ub, " @;
 put "ffs, " @;
 put "ffs_lb, " @;
 put "ffs_ub, " @;
 put "size, " @;
 put "wslope, " @;
 put "wint, " @;
 put "mslope, " @;
 put "mint, " @;
 put "forprofit";

end;

 put hmo "," hmo_lb "," hmo_ub "," ffs ","
 ffs_lb "," ffs_ub "," size "," wslope "," wintercept
 "," mslope "," mintercept "," forprofit;





%mend m;

%m(card_ed_vis)
%m(card_ip_vis)
%m(card_em_cost)
%m(card_ip_cost)
%m(card_surg_cost)

%m(diab_ed_vis)
%m(diab_ip_vis)
%m(diab_em_cost)
%m(diab_ip_cost)
%m(diab_surg_cost)



%mend yy;

%yy(9)


proc print data=rg.allcovm;
proc print data=rg.allfm;



